package com.yzkj.siot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzkj.siot.context.CurUserCtx;
import com.yzkj.siot.domain.TenantDevice;
import com.yzkj.siot.mapper.TenantDeviceMapper;
import com.yzkj.siot.service.TenantDeviceService;
import com.yzkj.siot.util.RedisUtil;
import com.yzkj.siot.vo.RadioDataItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 设备
 * 服务实现类
 * </p>
 *
 * @author skye
 * @since 2021-11-07
 */
@Service
public class TenantDeviceServiceImpl extends ServiceImpl<TenantDeviceMapper, TenantDevice> implements TenantDeviceService {

    @Autowired
    RedisUtil redisUtil;

    @Override
    public List<RadioDataItem> queryDeviceOfProductForRadio() {

        return baseMapper.queryDeviceOfProductForRadio(CurUserCtx.get().getTenantId());
    }

    @Override
    public int onlineDeviceCount() {

        List<String> keys = redisUtil.keys("tenant_device_data:" + CurUserCtx.get().getTenantId() + ":*");

        return CollectionUtils.isEmpty(keys) ? 0 : keys.size();
    }
}
